<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Forum Model
 *
 * @package		SimplyPost
 * @subpackage	Forum
 * @category	Model
 * @author		Pascal Kriete
 */

class For_model extends Model {
	
	/**
	 * Constructor
	 *
	 * @access	public
	 */
    function For_model()
    {
        parent::Model();
    }

	// --------------------------------------------------------------------

	/**
	 * Get parent information
	 *
	 * @access	public
	 * @param	integer	forum id
	 * @return	mixed	parent info
	 */
	function get_info($id)
	{
		$this->db->select('id, name');
		$this->db->from('categories');
		$this->db->where('id', $id);
		$query = $this->db->get('categories');
		
        if ($query->num_rows < 1)
            return FALSE;
		else
			return $query->result_array();
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Get one forum
	 *
	 * @access	public
	 * @param	integer	forum id
	 * @return	mixed	forum data
	 */
	function get($id)
	{
		$query = $this->db->get_where('forums', array('id' => $id), 1, 0);
		return $query->result_array();
	}
	
	// --------------------------------------------------------------------
    
	/**
	 * Get all forums in one category
	 *
	 * @access	public
	 * @param	integer	parent category id
	 * @return	mixed	category sub forums
	 */
    function get_forums($id)
    {
		$this->db->where('parent_id', $id);
		$query = $this->db->get('forums');
		
		if ($query->num_rows < 1)
            return array(-1);
		else
			return $query->result_array();
    }

	// --------------------------------------------------------------------

	/**
	 * Create a new forum
	 *
	 * @access	public
	 * @param	string	new forum name
	 * @param	string	new forum description
	 * @param	string	parent id
	 * @return	void
	 */
	function create($content, $desc, $id)
	{
		$this->db->insert('forums', array(
			'name' 			=> $content,
			'description' 	=> $desc,
			'parent_id' 	=> $id
		));
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Update forum information
	 *
	 * @access	public
	 * @param	integer	forum id
	 * @param	mixed	forum contents
	 * @return	void
	 */
	function update($id, $data)
	{
		$this->db->where('id', $id);
		$this->db->update('forums', $data);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Delete a forum and all children
	 *
	 * @access	public
	 * @param	integer	forum id
	 * @return	void
	 */
	function delete($id) {
		$this->db->select('id, parent_id');
		$this->db->where('parent_id', $id);
		$query = $this->db->get('topics');
		
		foreach ($query->result_array() as $row)
		{
			$this->db->delete('posts', array('parent_id' => $row['id']));
		}
		
		$this->db->delete('topics', array('parent_id' => $id));
		$this->db->delete('forums', array('id' => $id));
	}
}